package com.surelution.projects.crm

class StationGasController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [stationGasInstanceList: StationGas.list(params), stationGasInstanceTotal: StationGas.count()]
    }

    def create = {
        def stationGasInstance = new StationGas()
        stationGasInstance.properties = params
        return [stationGasInstance: stationGasInstance]
    }

    def save = {
		def gasTypeList = params.gasType.id
		println gasTypeList
		if (gasTypeList != null){
			gasTypeList.each{type->
				def stationGasInstance = new StationGas()
				def station = GasStation.get(params.station.id)
				def gasType = GasType.get(type)
				def stationGas = StationGas.findByStationAndGasType(station, gasType)
				if (!stationGas){
					stationGasInstance.updateTime = new Date()
					stationGasInstance.active = false
					if (params.active!=null){
						stationGasInstance.active = params.active
						}
					stationGasInstance.gasType = gasType
					stationGasInstance.station = station
					if (stationGasInstance.save(flush: false)) {
			            render(view: "create", model: [stationGasInstance: stationGasInstance])
			        }		
				}
		    }
			flash.message = "${message(code: 'stationGas.created.message')}"
		}
		if (gasTypeList == null){
			flash.message = "${message(code: 'default.not.created.message')}"
			}
		
		def station = GasStation.get(params.station.id)
		if(station) {
			if(station instanceof OwnStation) {
				redirect(controller: "ownStation",action: "show",id: params.station.id)
			} else if(station instanceof CompetitorStation) {
				redirect(controller: "competitorStation",action: "show",id: params.station.id)
			}
		}
    }

    def show = {
        def stationGasInstance = StationGas.get(params.id)
        if (!stationGasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationGas.label', default: 'StationGas'), params.id])}"
            redirect(action: "list")
        }
        else {
            [stationGasInstance: stationGasInstance]
        }
    }

    def edit = {
        def stationGasInstance = StationGas.get(params.id)
        if (!stationGasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationGas.label', default: 'StationGas'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [stationGasInstance: stationGasInstance]
        }
    }

    def update = {
        def stationGasInstance = StationGas.get(params.id)
        if (stationGasInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (stationGasInstance.version > version) {
                    stationGasInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'stationGas.label', default: 'StationGas')] as Object[], "Another user has updated this StationGas while you were editing")
                    render(view: "edit", model: [stationGasInstance: stationGasInstance])
                    return
                }
            }
            stationGasInstance.properties = params
			stationGasInstance.updateTime = new Date()
			if (!stationGasInstance.hasErrors() && stationGasInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'stationGas.label', default: 'StationGas'), stationGasInstance.id])}"
                redirect(action: "show", id: stationGasInstance.id)
            }
            else {
                render(view: "edit", model: [stationGasInstance: stationGasInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationGas.label', default: 'StationGas'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def stationGasInstance = StationGas.get(params.id)
        if (stationGasInstance) {
            try {
                stationGasInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'stationGas.label', default: 'StationGas'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'stationGas.label', default: 'StationGas'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationGas.label', default: 'StationGas'), params.id])}"
            redirect(action: "list")
        }
    }
}
